package cn.jr.dao.impl;

import cn.jr.dao.IActiveDao;
import cn.jr.pojo.Active;
import cn.jr.util.DBHelper;
import cn.jr.util.PageHelper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedList;

@SuppressWarnings("all")
public class ActiveDaoImpl implements IActiveDao {

    DBHelper db = new DBHelper();

    @Override //添加活动
    public int insertActive(Active active) {
        String sql = "insert into tactive values(null,?,?,?,?)";
        Object[] objs = {active.getActiveName(),active.getActiveStartTime(),active.getActiveEndTime(),active.getActiveMoney()};
        return db.dtUpdate(sql, objs);
    }

    @Override //查询活动 -- 全部
    public ArrayList<Active> selectAllActive() {
        String sql = "select * from tactive";
        ArrayList<Active> actives = (ArrayList<Active>) db.dtQuery(sql, Active.class);
        return actives;
    }

    @Override //查询活动 -- 单个
    public Active selectActiveById(Integer activeId) {
        String sql = "select * from tactive where activeId=?";
        ArrayList<Active> actives = (ArrayList<Active>) db.dtQuery(sql, Active.class, activeId);
        return actives.size()>0?actives.get(0):new Active();
    }

    @Override //修改活动
    public int updateActive(Active active) {
        String sql = "update tactive set activeName=?,activeStartTime=?,activeEndTime=?,activeMoney=? where activeId=?";
        Object[] objs = {active.getActiveName(),active.getActiveStartTime(),active.getActiveEndTime(),active.getActiveMoney(),active.getActiveId()};
        return db.dtUpdate(sql, objs);
    }

    @Override //删除活动
    public int deleteActive(Active active) {
        String sql = "delete from tactive where activeId=?";
        return db.dtUpdate(sql, active.getActiveId());
    }


    //分页
    @Override //查询总条数
    public int selectCountByActiveAndPageHelper() {
        String sql = "select count(*) from tactive";
        /*//没用的
        ArrayList<Active> actives = (ArrayList<Active>) db.dtQuery(sql, Active.class);
        LinkedList ll = new LinkedList();
        for(Active active : actives) {
            ll.add(active.getActiveId());
        }*/

        Connection con = db.getCon();
        PreparedStatement ps = null;
        ResultSet resultSet = null;
        try {
            ps = con.prepareStatement(sql.toString());
            resultSet = ps.executeQuery();
            //判断，有这一行数据就拿第一列（列数是从1开始的）
            return resultSet.next() ? resultSet.getInt(1) : 0;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            db.closeAll(con, ps, resultSet);
        }
        return 0;
    }

    @Override //查询当前页面的集合数据
    public ArrayList<Active> selectByActive(PageHelper ph) {
        String sql = "select * from tactive limit ?,?";
        Active active = new Active();
//        ArrayList<Active> actives = (ArrayList<Active>) db.dtQuery(sql, Active.class);
        LinkedList ll = new LinkedList();
        /*for(Active active : actives) {
            ll.add(active.getActiveId());
        }*/
        //当前页面的第一条数据，因为第一页的第一条数据下标是0，得减一
        ll.add((ph.getPageIndex()-1)*ph.getShowNumber());//对应第一个问号
        ll.add(ph.getShowNumber());//对应第二个问号

        return db.dtQuery(sql.toString(),active.getClass(),ll.toArray());
    }
}
